//
//  EFSearchTopCell.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/30.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFSearchTopCell.h"
#import "EFSearchCategoryItem.h"

@interface EFSearchTopCell ()
@property (strong, nonatomic) UIButton *priceOrderBtn;
@property (strong, nonatomic) UIButton *productsBtn;
@property (strong, nonatomic) UIButton *categoryBtn;
@property (strong, nonatomic) UIButton *saleBtn;

@property (strong, nonatomic) NSMutableArray *categoryArray;
@property (assign, nonatomic) NSInteger topType;
@end

@implementation EFSearchTopCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = RGBCOLOR(0xf5f9f9);
        self.categoryArray = [NSMutableArray array];
        [self initUI];
    }
    return self;
}

- (void)initUI{
    self.priceOrderBtn = [self createBtn:@"价格排序"];
    self.productsBtn = [self createBtn:@"销售排序"];
    self.categoryBtn = [self createBtn:@"全部分类"];
    self.saleBtn = [self createBtn:@"促销"];
    
    [self addSubview:self.priceOrderBtn];
    [self addSubview:self.productsBtn];
    self.productsBtn.left = self.priceOrderBtn.right;
    
    [self addSubview:self.categoryBtn];
    self.categoryBtn.left = self.productsBtn.right;
    [self addSubview:self.saleBtn];
    self.saleBtn.left = self.categoryBtn.right;
    
    self.priceOrderBtn.selected = YES;
}

- (void)setObject:(NSArray *)categorys{
    if (categorys.count > 0) {
        self.categoryArray = [NSMutableArray arrayWithArray:categorys];
//        EFSearchCategoryItem *item =categorys[0];
//        [self.categoryBtn setTitle:item.catName forState:UIControlStateNormal];
        UILabel *tempLabel = [[UILabel alloc] init];
        tempLabel.font = [UIFont systemFontOfSize:13];
        tempLabel.text = @"全部分类";
        [tempLabel sizeToFit];
        [self.categoryBtn setImageEdgeInsets:UIEdgeInsetsMake(0,tempLabel.width,0,-tempLabel.width)];
    }
}

- (UIButton *)createBtn:(NSString *)text{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth/4, 32)];
    [btn setTitle:text forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn setTitleColor:kBlack333 forState:UIControlStateNormal];
    [btn setTitleColor:RGBCOLOR(0x69ba45) forState:UIControlStateSelected];
    [btn setImage:IMG(@"dropdown_icon_default") forState:UIControlStateNormal];
    [btn setImage:IMG(@"dropdown_icon_selected") forState:UIControlStateSelected];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -11, 0, 11)];
    UILabel *tempLabel = [[UILabel alloc] init];
    tempLabel.font = [UIFont systemFontOfSize:13];
    tempLabel.text = text;
    [tempLabel sizeToFit];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0,tempLabel.width,0,-tempLabel.width)];
    
    [btn layoutIfNeeded];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    return btn;
}

- (void)btnClick:(UIButton *)btn{
    btn.selected = YES;
    NSMutableArray *array = [NSMutableArray array];
    if (btn == self.priceOrderBtn) {
        self.productsBtn.selected = NO;
        self.categoryBtn.selected = NO;
        self.saleBtn.selected = NO;
        [array addObject:@"从低到高"];
        [array addObject:@"从高到低"];
        self.topType = 1;
    }else if(btn == self.productsBtn){
        self.priceOrderBtn.selected = NO;
        self.categoryBtn.selected = NO;
        self.saleBtn.selected = NO;
        [array addObject:@"从低到高"];
        [array addObject:@"从高到低"];
        self.topType = 2;
    }else if(btn == self.categoryBtn){
        self.priceOrderBtn.selected = NO;
        self.productsBtn.selected = NO;
        self.saleBtn.selected = NO;
        array = self.categoryArray;
        self.topType = 3;
    }else if(btn == self.saleBtn){
        self.priceOrderBtn.selected = NO;
        self.productsBtn.selected = NO;
        self.categoryBtn.selected = NO;
        [array addObject:@"促销"];
        self.topType = 4;
    }
    if (self.type != nil) {
        self.type(array,self.topType);
    }
}



@end
